iT邦幫忙

2021 iThome 鐵人賽

DAY 12
0

繼續開工~intent 的搜尋關鍵字-android intent action

https://developer.android.com/guide/components/intents-filters?hl-zh-tw#java

電話跟簡訊模擬器不能測試.因為沒有綁電信....


動畫-老師說沒有人會花很多時間在看動畫的!!!只會播一次

先建立XML檔排版-

https://ithelp.ithome.com.tw/upload/images/20220201/20119035jOTK4w6uh1.png

button也是要綁定onclick

https://ithelp.ithome.com.tw/upload/images/20220201/201190352qSOXKuNKz.png

還沒有onclick就會反紅
https://ithelp.ithome.com.tw/upload/images/20220201/20119035ohRaZBteiT.png

package com.huang.myapplication9;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onclick(View view) {
    }
}

改照片的地方-Common Attributes-srcCompat
https://ithelp.ithome.com.tw/upload/images/20220201/20119035hMDZ6qNGR8.png

要往下選圖模擬器才可以看到

https://ithelp.ithome.com.tw/upload/images/20220201/201190354spep9eUkz.png

https://ithelp.ithome.com.tw/upload/images/20220201/20119035ENWJWcBcuy.png

https://ithelp.ithome.com.tw/upload/images/20220201/20119035LLy9Apl0DP.png


新增-資源類資料夾

https://ithelp.ithome.com.tw/upload/images/20220201/20119035EI5aUYoKbN.png

開始~

https://ithelp.ithome.com.tw/upload/images/20220201/20119035m5ODBtYS6x.png

選anim產生動畫資料夾

https://ithelp.ithome.com.tw/upload/images/20220201/201190353gbCs428K6.png

選長這樣~按OK

https://ithelp.ithome.com.tw/upload/images/20220201/20119035bf7vVi9wdg.png

https://ithelp.ithome.com.tw/upload/images/20220201/20119035yMEDOXkLvN.png

在anim上選anim... resoure file 會跳出

https://ithelp.ithome.com.tw/upload/images/20220201/20119035NAVIx918Zg.png

Root element的set是動畫"組"的意思

https://ithelp.ithome.com.tw/upload/images/20220201/20119035WEKnGOlmID.png

https://ithelp.ithome.com.tw/upload/images/20220201/20119035gvwa6UUSpf.png


動畫的內容就是由小變到大

也是包頭尾同
https://ithelp.ithome.com.tw/upload/images/20220201/20119035fP88lPcF4R.png

單位不寫就是以%計算
android:pivotX="50%"一定要寫%

duration要自己打要以毫秒來寫..1秒=1000毫秒

預設是300毫秒

android:duration="2000"

https://ithelp.ithome.com.tw/upload/images/20220201/20119035yz0uCA31Ko.png

<scale
         android:fromXScale="1.0"
         android:fromYScale="1.0"
         android:toXScale="3.0"
         android:toYScale="3.0"
         android:pivotX="50%"
         android:pivotY="50%"
         android:duration="2000"
       />

動畫的內容就是由小變到大..再由大變到小

anim01.xml程式碼:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
     <!-- 縮放 -->
     <scale
         android:fromXScale="1.0"
         android:fromYScale="1.0"
         android:toXScale="3.0"
         android:toYScale="3.0"
         android:pivotX="50%"
         android:pivotY="50%"
         android:duration="2000"
       />
     <scale
         android:fromXScale="3.0"
         android:fromYScale="3.0"
         android:toXScale="1.0"
         android:toYScale="1.0"
         android:pivotX="50%"
         android:pivotY="50%"
         android:duration="2000"
         />

</set>

用ctrl c+ctrl v 產生3個anim01.xml

https://ithelp.ithome.com.tw/upload/images/20220201/20119035IOZVe3RZML.png
再來一個一個改~
anim02.xml程式碼:呈現旋轉 角度0 正轉-寫一組就好不然沒反應

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
     <!-- 旋轉 -->
     <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotY="50%"
        android:pivotX="50%"
        android:duration="2000"
        />
    

</set>


anim03.xml程式碼: 移動+透明度(1.0可看見+0.0看不見)-只會播一次

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
     <!-- 移動 -->
     <translate
         android:fromXDelta="20"
         android:fromYDelta="100"
         android:toXDelta="500"
         android:toYDelta="300"
         android:duration="2000"
          />
     <translate
         android:fromXDelta="100"
         android:fromYDelta="100"
         android:toXDelta="20"
         android:toYDelta="100"
         android:duration="2000"
         />
     <!-- 透明度 -->
     <alpha
         android:fromAlpha="1.0"
         android:toAlpha="0.3"
         android:duration="2000"
          />
     <alpha
         android:fromAlpha="0.3"
         android:toAlpha="1.0"
         android:duration="2000"
         />

</set>
     



還沒有寫onClick所以還不會動-

功能-java檔

getDuration()定義影片跑多久

使用switch .. case

package com.huang.myapplication9;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onclick(View view) {
        switch (view.getId()){
            case R.id.button1:
                
                break;
            case R.id.button2:

                break;
            case R.id.button3:

                break;    
        }


    }
}

要先初始化-ImageView

ImageView img =findViewById(R.id.imageView1);

右邊做完再到左邊

switch (view.getId()){
            case R.id.button1:
                Animation anim1 =AnimationUtils.loadAnimation(this,R.anim.anim01);
                img.startAnimation(anim1);

                break;

https://ithelp.ithome.com.tw/upload/images/20220201/201190351mYN3qePsZ.png


完整程式碼:

package com.huang.myapplication9;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onclick(View view) {

        ImageView img =findViewById(R.id.imageView1);

        switch (view.getId()){
            case R.id.button1:
                Animation anim1 =AnimationUtils.loadAnimation(this,R.anim.anim01);
                img.startAnimation(anim1);

                break;
            case R.id.button2:
                Animation anim2 =AnimationUtils.loadAnimation(this,R.anim.anim02);
                img.startAnimation(anim2);

                break;
            case R.id.button3:
                Animation anim3 =AnimationUtils.loadAnimation(this,R.anim.anim03);
                img.startAnimation(anim3);

                break;
        }


    }
}

可以用模擬器測試


BUTTON2的星星與法改這樣更美

case R.id.button2:
                ObjectAnimator obj = ObjectAnimator.ofFloat(img, "rotation", 0, 360, 0);
                obj.setDuration(2000);
                obj.start();
                

完整程式碼:

package com.huang.myapplication9;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onclick(View view) {

        ImageView img =findViewById(R.id.imageView1);

        switch (view.getId()){
            case R.id.button1:
                Animation anim1 =AnimationUtils.loadAnimation(this,R.anim.anim01);
                img.startAnimation(anim1);

                break;
            case R.id.button2:
                ObjectAnimator obj = ObjectAnimator.ofFloat(img, "rotation", 0, 360, 0);
                obj.setDuration(2000);
                obj.start();

                break;
            case R.id.button3:
                Animation anim3 =AnimationUtils.loadAnimation(this,R.anim.anim03);
                img.startAnimation(anim3);

                break;
        }


    }
}

先全部關掉再做-
https://ithelp.ithome.com.tw/upload/images/20220201/20119035SPr1Hf2BNj.png

從新增模組-從app-new-Module

https://ithelp.ithome.com.tw/upload/images/20220201/20119035hJwNgeWjWB.png

選針對手機+名稱
https://ithelp.ithome.com.tw/upload/images/20220201/20119035JABrZtlmtB.png

長出來在-

https://ithelp.ithome.com.tw/upload/images/20220201/20119035h5oAAuQGrT.png

在旁邊長出來-
https://ithelp.ithome.com.tw/upload/images/20220201/201190359ySoaV3Rhu.png

用套件做作-套件網址:https://github.com/daimajia/AndroidViewAnimations
https://ithelp.ithome.com.tw/upload/images/20220201/20119035758odLsl2V.png
第一步選Gradle-

https://ithelp.ithome.com.tw/upload/images/20220201/20119035ho2raKy5CW.png

貼上-

implementation 'com.daimajia.androidanimations:library:2.4@aar'

https://ithelp.ithome.com.tw/upload/images/20220201/201190357HzX608jf9.png

要同步載入

https://ithelp.ithome.com.tw/upload/images/20220201/20119035ePscqFvcA6.png


加入兩個元件

https://ithelp.ithome.com.tw/upload/images/20220201/20119035JIggH9qYNr.png

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="Hello World!"
        android:textColor="?android:attr/colorFocusedHighlight"
        android:textSize="36sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text" />

</androidx.constraintlayout.widget.ConstraintLayout>

改了顏色就會異常消失

https://ithelp.ithome.com.tw/upload/images/20220201/20119035JTwLpxzDgP.png

https://ithelp.ithome.com.tw/upload/images/20220201/20119035jukAvUYB3P.jpg

https://ithelp.ithome.com.tw/upload/images/20220201/20119035cYCYJTbCIM.jpg

https://ithelp.ithome.com.tw/upload/images/20220201/201190356r9zKoNqJb.jpg


直接到JAVA檔

1-初始化
https://ithelp.ithome.com.tw/upload/images/20220202/201190352qz1D0nRrI.png

2-加偵聽器-onClick是從偵聽器加的不是BUTTON的onClick

https://ithelp.ithome.com.tw/upload/images/20220202/20119035BPwZuFn1Gn.png

https://ithelp.ithome.com.tw/upload/images/20220202/20119035vFTVYHI8vg.png

package com.huang.myanim2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button button;
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = findViewById(R.id.text);
        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                
            }
        });
            }
}

再來是Step 2
https://ithelp.ithome.com.tw/upload/images/20220202/20119035EK3p4YugPh.png

https://ithelp.ithome.com.tw/upload/images/20220202/20119035rt4nrUBaZj.png

findViewById(R.id.edit_area) 要改成text就是讓文字能動

https://ithelp.ithome.com.tw/upload/images/20220202/20119035Zp99NKh0Ov.png

package com.huang.myanim2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;

public class MainActivity extends AppCompatActivity {

    Button button;
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = findViewById(R.id.text);
        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                YoYo.with(Techniques.Tada)//要按alt+enter
                        .duration(700)//持續的時間
                        .repeat(5)//重複的次數
                        .playOn(text);

            }
        });
            }
}

然後要換其他效果就是改Tada

YoYo.with(Techniques.Tada)//要按alt+enter.然後要換其他效果就是改Tada
                        .duration(700)//持續的時間
                        .repeat(5)//重複的次數
                        .playOn(text);

這裡例如改縮放-ZoomInLeft

https://ithelp.ithome.com.tw/upload/images/20220202/20119035GG7D4XHlO5.png

https://ithelp.ithome.com.tw/upload/images/20220202/20119035yRULWXfzJL.png

YoYo.with(Techniques.ZoomInLeft)//要按alt+enter
                        .duration(700)//持續的時間
                        .repeat(5)//重複的次數
                        .playOn(text);


上一篇
第11天~
下一篇
第13天~
系列文
就是從無到有寫app31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言